package com.example.service.appointment;


import cn.hutool.core.util.IdUtil;
import com.example.mapper.appointment.MedicalCardMapper;
import com.example.model.entity.appointment.MedicalCard;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 病例表业务处理
 **/
@Service
public class MedicalCardService {

    @Resource
    private MedicalCardMapper medicalCardMapper;

    /**
     * 新增
     */
    public void add(MedicalCard medicalCard) {
        medicalCard.setMedicalNum(IdUtil.getSnowflakeNextIdStr());
        medicalCardMapper.insert(medicalCard);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        medicalCardMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            medicalCardMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(MedicalCard medicalCard) {
        medicalCardMapper.updateById(medicalCard);
    }

    /**
     * 根据ID查询
     */
    public MedicalCard selectById(Integer id) {
        return medicalCardMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<MedicalCard> selectAll(MedicalCard medicalCard) {
        return medicalCardMapper.selectAll(medicalCard);
    }

    /**
     * 分页查询
     */
    public PageInfo<MedicalCard> selectPage(MedicalCard medicalCard, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MedicalCard> list = medicalCardMapper.selectAll(medicalCard);
        return PageInfo.of(list);
    }

    public MedicalCard selectByRecordId(Integer recordId) {
        return medicalCardMapper.selectByRecordId(recordId);
    }

    public String selectByMedicalNum(String medicalNum) {
        MedicalCard medicalCard = medicalCardMapper.selectByMedicalNum(medicalNum);

        // 检查medicalCard是否为null，以避免NullPointerException
        if (medicalCard == null) {
            return null;
        }

        // 获取医疗卡中的疾病描述、医生建议和治疗计划
        String diseaseDescription = medicalCard.getDiseaseDescription();
        String doctorTalk = medicalCard.getDoctorTalk();
        String treatmentPlan = medicalCard.getTreatmentPlan();

        // 拼接字符串信息
        String result = "疾病描述：" + diseaseDescription + "\n"
                + "医生建议：" + doctorTalk + "\n"
                + "治疗方案：" + treatmentPlan;

        return result;
    }
}
